import UAParser from 'ua-parser-js';
import axios from 'axios';
import { ref } from 'vue';
import FingerprintJS from '@fingerprintjs/fingerprintjs'
// 创建一个异步函数来获取设备信息和IP地址
export async function getDeviceInfo() {
    // 定义一个 ref 变量来存储IP地址
    const ip = ref(null);
    const fp = await FingerprintJS.load()
    const fpResult = await fp.get()
    const visitorId = fpResult.visitorId
    // console.log('device_info: store = ', store)
    // console.log('device_info: 访客标识fp = ', fp)
    // console.log('device_info: 访客标识fpResult = ', fpResult)
    // console.log('device_info: 访客标识visitorId = ', visitorId)
    try {
        // 使用 await 获取IP地址
        const ipResponse = await axios.get('https://api.ipify.org?format=json');
        ip.value = ipResponse.data.ip;
        console.log("device_info: ipResponse.data.ip = ", ipResponse.data.ip);
    } catch (error) {
        console.error('device_info: Error fetching IP address:', error);
    }

    // 使用 UAParser 获取设备信息
    const parser = new UAParser();
    const result = parser.getResult();

    // 返回包含IP地址和设备信息的对象
    return {
        parser: result,
        browser: result.browser.name + ' ' + result.browser.version,
        os: result.os.name + ' ' + result.os.version,
        device: result.device.vendor + ' ' + result.device.model,
        ip: ip.value,
        visitorId: visitorId,
    };
}
